package com.lbn.jfqsc.app.home.detail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lbn.baseframe.base.BaseActivity;
import com.lbn.baseframe.ui.NAppbar;
import com.lbn.baseframe.utils.DateUtils;
import com.lbn.baseframe.utils.EasyToast;
import com.lbn.jfqsc.App;
import com.lbn.jfqsc.R;
import com.lbn.jfqsc.app.alipay.AlipayActivity;
import com.lbn.jfqsc.app.login.LoginActivity;
import com.lbn.jfqsc.business.bean.Good;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DetailActivity extends BaseActivity<DetailPresenter> implements IDetailView {

    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.place)
    TextView place;
    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.collect)
    CheckBox collect;
    @BindView(R.id.activity_detail)
    RelativeLayout activityDetail;
    private int id;
    private String price;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        ButterKnife.bind(this);
        setAppbar(new NAppbar(this));
        appbar.setTitle("详情");
        appbar.openBack();
        id = getIntent().getIntExtra("id", 0);

    }


    @Override
    public DetailPresenter createPresenter() {
        return new DetailPresenter();
    }

    @Override
    public void onPresenterCreated() {
        super.onPresenterCreated();
        mPresenter.getGood(id);
    }

    public static void startActivity(Context context,int id) {
        Intent intent = new Intent(context, DetailActivity.class);
        intent.putExtra("id",id);
        context.startActivity(intent);
    }


    @Override
    public void setGood(Good good) {
        userName.setText(good.getUserName());
        time.setText("发布时间 : "+DateUtils.stampToDate(good.getDate()));
        phone.setText("联系电话 : "+good.getGoodsPhone());
        place.setText("联系地址 : "+good.getGoodsPlace());
        content.setText(good.getGoodsIntroduce());
        price = good.getGoodsPrice();
//        if(){
//
//        }
    }

    @Override
    public void onError(String error) {
        EasyToast.showShort(this, error);
    }

    @Override
    public void setCollection(boolean b) {
        if(b){
            collect.setText("已收藏");
            collect.setEnabled(false);
            collect.setFocusable(false);
        }else{
            collect.setText("未收藏");
        }
        collect.setChecked(b);
    }

    @OnClick({R.id.collect, R.id.alipay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.collect:
                if(!App.isLogin()){
                    collect.setChecked(false);
                    LoginActivity.startActivity(this);
                    return;
                }
                mPresenter.collection(App.getUser().getId(),id);
                break;
            case R.id.alipay:
                if(!App.isLogin()){
                    LoginActivity.startActivity(this);
                    return;
                }
                AlipayActivity.startActivity(this,id,price,1);
                break;
        }
    }
}
